package org.xiaojl.utils;

import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

/**
 * JSON处理的工具类
 */
public class JsonUtil {
	private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
	
	public static String obj2str(Object obj){
		Gson gson = new GsonBuilder().create();
		String res = gson.toJson(obj);
		return res;
	}
	
	public static T str2obj(String str,Class<T> classOfT){
		try {
			Gson gson = new GsonBuilder().create();
			return gson.fromJson(str, classOfT);
		} catch (JsonSyntaxException e) {
			log.error("Json字符串转对象出现异常：", e);
			return null;
		}
	}
	
}
